package com.hutao.manage.util;

import java.math.BigDecimal;

public class BigDecimalUtil {
	
	/**
	 * 比较数据大小
	 * @param b1
	 * @param b2
	 * @return
	 * 		结果为0相等 1大于 -1 小于
	 * 
	 */
	public static String getCompareResult(BigDecimal b1,BigDecimal b2) {
		String result = null;
		if(b1.compareTo(b2)==0) {
			result = "相等";
		}else if(b1.compareTo(b2)==-1) {
			result = "减少";
		}else {
			result = "增加";
		}
		return result;
	}
	
	
	
	/**
	 * 数据上升或下降
	 * @param b1
	 * @param b2
	 * @return
	 * 		结果为0相等 1大于 -1 小于
	 * 
	 */
	public static String getCompareUpDowm(BigDecimal b1,BigDecimal b2) {
		String result = null;
		if(b1.compareTo(b2)==0) {
			result = "相等";
		}else if(b1.compareTo(b2)==-1) {
			result = "下降";
		}else {
			result = "上升";
		}
		return result;
	}
	
	
	
	/**
	 * 计算两值的差值
	 * @param b1
	 * @param b2
	 * @return
	 */
	public static BigDecimal getNumDifference(BigDecimal b1,BigDecimal b2) {
		if(b1.compareTo(b2)==0) {
			return b1.subtract(b2);
		}else if(b1.compareTo(b2)==-1) {
			return b2.subtract(b1).setScale(2, BigDecimal.ROUND_HALF_UP);
		}else {
			return b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP);
		}
		
	}
	
	public static BigDecimal getNumDifference(BigDecimal b1,BigDecimal b2, boolean noPrefix) {
		if(b1.compareTo(b2)==0) {
			return b1.subtract(b2);
		}else if(b1.compareTo(b2)==-1) {
			return b2.subtract(b1).setScale(0, BigDecimal.ROUND_HALF_UP);
		}else {
			return b1.subtract(b2).setScale(0, BigDecimal.ROUND_HALF_UP);
		}
		
	}
	
	
	
	public static BigDecimal getSubtract(BigDecimal b1,BigDecimal b2, int scale) {
		
		return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP);
	}
	
	
	
	
	
	
	
	
	
	/**
	 * 比较数据大小
	 * @param b1
	 * @param b2
	 * @return
	 * 		结果为0相等 1大于 -1 小于
	 * 
	 */
	public static String getCompareRateResult(BigDecimal b1,BigDecimal b2) {
		String result = null;
		if(b1.compareTo(b2)==0) {
			result = "相等";
		}else if(b1.compareTo(b2)==-1) {
			result = "减幅";
		}else {
			result = "增幅";
		}
		return result;
	}
	
	
	public static BigDecimal getCompareRate(BigDecimal b1,BigDecimal b2) {
		
		BigDecimal result = null;
		if(b1.compareTo(b2)==0) {
			result = new BigDecimal(0.0);
		}else if(b1.compareTo(b2)==-1) {
			result = (b2.subtract(b1)).divide(b2, 4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2);
		}else {
			result = (b1.subtract(b2)).divide(b2, 4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2);
		}
		return result;
	}
	
	public static BigDecimal getCompareRate(BigDecimal b1,BigDecimal b2,int scale) {
		BigDecimal result = null;
		if(b1.compareTo(b2)==0) {
			result = new BigDecimal(0.0);
		}else if(b1.compareTo(b2)==-1) {
			result = (b2.subtract(b1)).divide(b2, 4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(scale);
		}else {
			result = (b1.subtract(b2)).divide(b2, 4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(scale);
		}
		return result;
	}
	
	
	public static BigDecimal getHbRate(BigDecimal b1,BigDecimal b2) {
		BigDecimal result = null;
		if(b1.compareTo(b2)==0) {
			result = new BigDecimal(0.0);
		}else {
			result = (b1.subtract(b2)).divide(b2, 4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2);
		}
		return result;
	}
	
	
	
	

	
	
	public static BigDecimal getDivideRate(BigDecimal b1,BigDecimal b2) {
		BigDecimal result = null;
		
		result = b1.divide(b2, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2);
		
		return result;
	}
	
	
	public static BigDecimal getDivide(BigDecimal b1,BigDecimal b2) {
		BigDecimal result = null;
		
		result = b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP);
		
		return result;
	}
	
	public static BigDecimal getDivide(BigDecimal b1,BigDecimal b2, int scale) {
		BigDecimal result = null;
		
		result = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
		
		return result;
	}
	
	
	
	
	
	public static BigDecimal getScale(BigDecimal a, int scale) {
		return a.setScale(scale,BigDecimal.ROUND_HALF_UP);
	}
	
	
	 /**    
     *   提供精确的减法运算。    
     *   @param   v1   被减数    
     *   @param   v2   减数    
     *   @return   两个参数的差    
     */     
 
   public   static   Double   sub(Double   v1,Double   v2){     
	      if(v1==null || v2==null) {
	    	  return null;
	      }
           BigDecimal   b1   =   new   BigDecimal(Double.toString(v1));     
           BigDecimal   b2   =   new   BigDecimal(Double.toString(v2));     
           return   b1.subtract(b2).doubleValue();     
   }       
   
   
   /**    
    *   提供精确的减法运算。    
    *   @param   v1   被减数    
    *   @param   v2   减数    
    *   @return   两个参数的差    
    */     

  public   static   Double   divide(Double   v1,Double   v2){     
	      if(v1==null || v2==null) {
	    	  return null;
	      }
          BigDecimal   b1   =   new   BigDecimal(Double.toString(v1));     
          BigDecimal   b2   =   new   BigDecimal(Double.toString(v2)); 
          BigDecimal sub =  b1.subtract(b2);
          return    sub.divide(b2, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2).doubleValue();
   
  }       
	
	
	
	
	
	public static void main(String[] args) {
		/*//7.4971--7.631--8.2752
		BigDecimal b1=new BigDecimal(7.4971);
		BigDecimal b2=new BigDecimal(7.631);
		BigDecimal b3=new BigDecimal(8.2752);
		BigDecimal result = null;
		System.out.println(b2.subtract(b1).divide(b2,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2));
		System.out.println(b3.subtract(b1).divide(b3,4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2));*/
		
		BigDecimal b1=new BigDecimal(0.30);
		BigDecimal b2=new BigDecimal(0.38);
		
		System.out.println(getCompareRate(b1, b2));
		
	}
	

	
	

}
